using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapController : MonoBehaviour
{

    public GameObject superWallPer;


    private void Awake()
    {
        InitMap(5, 3);
    }

    /// <summary>
    /// cols 多少列
    /// rows 多少行
    /// </summary>
    private int Cols, Rows;

    private List<Vector2> nullPointsList = new List<Vector2>();

    public void InitMap(int cols, int rows)
    {
        Cols = cols;
        Rows = rows;
        CreateSuperWall();
        FindNullPoints();
        Debug.Log(nullPointsList.Count);
    }

    /// <summary>
    /// 生成实体墙
    /// </summary>
    private void CreateSuperWall()
    {
        // 控制生成列数量， 每间隔一个生成一个 所以
        for (int x = -Cols; x < Cols; x += 2)
        {
            for (int y = -Rows; y < Rows; y += 2)
            {
                SpawnSuperWall(new Vector2(x, y));
            }
        }

        // 生成外围墙 S
        for (int x = -(Cols + 2); x <= Cols; x++)
        {
            // 上边
            SpawnSuperWall(new Vector2(x, Rows));

            // 下边
            SpawnSuperWall(new Vector2(x, -Rows - 2));
        }

        for (int y = -(Rows + 2); y < Rows; y++)
        {
            // 左边
            SpawnSuperWall(new Vector2(-(Cols + 2), y));
            // 右边
            SpawnSuperWall(new Vector2(Cols, y));
        }

        // 生成外围墙 E
    }


    private void SpawnSuperWall(Vector2 vector2)
    {
        GameObject wall = Instantiate(superWallPer, transform);
        wall.transform.position = vector2;
    }
    /// <summary>
    /// 查找场景中所有的空点
    /// </summary>
    private void FindNullPoints()
    {
        for (int x = -(Cols + 1); x <= Cols - 1; x++)
        {
            if (-(Cols + 1) % 2 == x % 2)
            {
                for (int y = -(Rows + 1); y <= Rows - 1; y++)
                {
                    nullPointsList.Add(new Vector2(x, y));
                }
            }
            else
            {
                for (int y = -(Rows + 1); y <= Rows - 1; y += 2)
                {
                    nullPointsList.Add(new Vector2(x, y));
                }
            }
        }
    }
}
